﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockStar.GatherData;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace Other
{
    class Program
    {
        static void Main(string[] args)
        {
            updateJsjj();
            Console.Read();
        }

        public static void updateJsjj() 
        {
            string _sql = "select secucode,issueorg,SecuMarket from dbo.SECURITY WITH(NOLOCK) where secucategory='001001' and (SecuMarket='012001' or SecuMarket='012002' or SecuMarket='012003' or SecuMarket='012015')";
            StringBuilder result = new StringBuilder();
            using (System.Data.SqlClient.SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.StrConn, System.Data.CommandType.Text, _sql))
            {
                int i = 0;
                while (reader.Read())
                {
                    i++;
                    string mk = "SZ";
                    string mkcode = reader[2].ToString();
                    if (mkcode == "012001") 
                    {
                        mk = "SH";
                    }
                    string stockcode = reader[0].ToString();
                    string orgcode = reader[1].ToString();
                    string _url = string.Format("http://www.windin.com/home/stock/html/{0}.{1}.shtml?&t=1&q={0}", stockcode, mk);

                    string _Content = OperMethod.GetPageByUrl(_url, "Gb2312");

                    Match match = Regex.Match(_Content, "<td class=\"title2\">公司简介</td></tr><tr><td>(?<g1>[^<]*)</td>");

                    string gg = match.Groups["g1"].Value;
                    if (string.IsNullOrEmpty(gg)) 
                    {
                        result.AppendFormat("Code={0},Orgid={1}\r\n", stockcode, orgcode);
                    }

                    Console.WriteLine(string.Format("正在采集第{0}条信息",i));

                    //result.AppendFormat("update COMPANY_INFO set companyintro='{0}' where orgid='{1}'\r\n", gg.Replace("'", "''"), orgcode);
                }

                reader.Close();
            }

            string ss = result.ToString();

            #region 执行SQL
            //if (!string.IsNullOrEmpty(result.ToString()))
            //{
            //    using (SqlConnection conn = new SqlConnection(SQLHelper.StrConn))
            //    {
            //        conn.Open();
            //        using (SqlTransaction trans = conn.BeginTransaction())
            //        {
            //            try
            //            {
            //                SQLHelper.ExecuteNonQuery(trans, CommandType.Text, result.ToString(), null);
            //                trans.Commit();
            //                Console.WriteLine("公司简介更新完成!");
            //            }
            //            catch (Exception ex)
            //            {
            //                trans.Rollback();
            //                Console.WriteLine("公司简介更新异常:" + ex.Message);
            //                LogFile.WriteLog("公司简介更新异常:" + ex.Message);
            //            }
            //        }
            //    }
            //}
            #endregion
        }
    }
}
